import {Module, EventDispatcher} from "../framework.js"
import DataStorage from "../data-storage.js"
import * as WidgetEvent from "../widget/widget-event.js"
import WidgetGroup from "./widget-group.js"
import WidgetSelector from "./widget-selector.js"
import {WIDGET_TYPE, WIDGET_DISPLAY_MODE} from './widget-const.js';
//import PageEvent from '../page/page-event.js'
import WidgetTools from "./widget-tools.js"
//import D from "../delegator.js"

export default class WidgetController extends Module {
    constructor(dp) {
        super(dp);

        const _disp = EventDispatcher();
        this._widgetTools = new WidgetTools(_disp);
        this._widgetGroup = new WidgetGroup(_disp);
        this._widgetSelector = new WidgetSelector(_disp);
        this._pageContent = undefined;

        _disp.Subscribe([
            WidgetEvent.EVT_WIDGET_CHANGED_TO_VIEW,
            WidgetEvent.EVT_WIDGET_CHANGED_TO_EDIT,
        ], event => {
            this.Dispatch(event);// for toc to update;
        });

        _disp.Subscribe(WidgetEvent.EVT_WIDGET_DIRTY_CONTENT_CHANGED_TO_VIEW, event => {
            DataStorage.UpdateWidgetContent(this.PID, event.WID);
        });


        _disp.Subscribe(WidgetEvent.EVT_WIDGET_TOOL_DELETE, event => {
            DataStorage.DeleteWidgetContent(this.PID, event.WID);
            if(this._widgetGroup.DeleteWidget(event.WID)) {
                if(this._widgetGroup.count == 0) {
                    this._widgetSelector.Show(-1);
                }
            }
        });
        _disp.Subscribe(WidgetEvent.EVT_WIDGET_TOOL_CLOSE, _ => {
            this._widgetGroup.ChangeWidgetToViewMode();
        });
        _disp.Subscribe(WidgetEvent.EVT_WIDGET_TOOL_MOVE_UP, event => {
            this._widgetGroup.MoveHigher(event.WID);
        });
        _disp.Subscribe(WidgetEvent.EVT_WIDGET_TOOL_MOVE_DOWN, event => {
            this._widgetGroup.MoveLower(event.WID);
        });
        this.Subscribe(WidgetEvent.EVT_WIDGET_TOOL_FOCUS, event => {
            this._widgetGroup.ScrollToWidget(event.WID);
        });


        _disp.Subscribe(WidgetEvent.EVT_WIDGET_CREATE_BTN_CLICKED, event => {
            this._widgetSelector.Show(event.offset);
        });
        _disp.Subscribe(WidgetEvent.EVT_WIDGET_SELECTOR_ITEM_CLICKED, event => {
            DataStorage.CreateWidgetContent(this.PID, event.offset, event.widgetType);
        });

        const _rl = DataStorage.RegistListener;
        _rl(DataStorage.EVT_DATASTORAGE_WIDGET_CONTENT_CREATED, event => {
            const _w = this._widgetGroup.CreateWidget(event.widgetContent, WIDGET_DISPLAY_MODE.EDIT);
            this._widgetGroup.Insert(_w, event.offset);
            this._widgetGroup.FocusWidget(_w);
        })
    }

    get PID() {
        return this._pageContent ? this._pageContent.SUID : "";
    }

    Update(pContent) {
        this._pageContent = pContent;
        this._widgetGroup.Clear();
        if(pContent.length > 0) {
            //const _fn = D(this._ui).CacheRestoreStyle('display', 'none');
            pContent.data.ForEach((_wc) => {
                if(_wc.type == WIDGET_TYPE.TEXT
                    || _wc.type == WIDGET_TYPE.HTML
                    || _wc.type == WIDGET_TYPE.MARKDOWN
                    || _wc.type == WIDGET_TYPE.CODE
                ) {
                    const _w = this._widgetGroup.CreateWidget(_wc, WIDGET_DISPLAY_MODE.VIEW);
                    this._widgetGroup.Insert(_w, Number.MAX_SAFE_INTEGER);


                }
                return false;
            }, this);
            //_fn();
        } else {
            this._widgetSelector.Show(Number.MAX_SAFE_INTEGER);
        }
        this._widgetTools.Hide(null, true);
        this.Dispatch(WidgetEvent.WidgetAllCreated(this._widgetGroup._widgetCollection));
    }
}

